<template>
  <div class="conter">
    <!--  导航栏 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" />
    <div class="imgInfo">
      <van-image
        width="120"
        height="120"
        fit="cover"
        round
        :src="userInfo.photo"
      />
      <van-uploader :after-read="xgimg" />
    </div>
    <van-cell-group>
      <van-cell
        title="姓名"
        is-link
        :value="userInfo.name"
        @click="nameClick"
      />
      <van-cell
        title="性别"
        is-link
        :value="userInfo.gender ? '女' : '男'"
        @click="ispopup = true"
      />
      <van-cell
        title="生日"
        is-link
        :value="userInfo.birthday"
        @click="isbirthday = true"
      />
    </van-cell-group>

    <!-- 修改名称 -->
    <van-dialog
      v-model="isname"
      title="修改名称"
      show-cancel-button
      @confirm="xgname"
    >
      <van-field ref="name" v-model="nametext" />
    </van-dialog>

    <!-- 修改性别 -->
    <van-popup v-model="ispopup" position="bottom">
      <van-nav-bar title="修改性别" left-arrow @click-left="ispopup = false" />
      <van-cell title="男" is-link @click="xbClick(0)" />
      <van-cell title="女" is-link @click="xbClick(1)" />
    </van-popup>

    <!-- 修改生日 -->
    <van-popup v-model="isbirthday" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="isbirthday = false"
        @confirm="srClick"
      />

      <!-- w34 -->
    </van-popup>

    <!-- 裁剪图片 -->
    <div class="cropper-box" v-show="isimg">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120px"
        autoCropHeight="120px"
      ></vueCropper>
      <van-button type="primary" @click="qdxgimg">确认</van-button>
      <van-button type="info" @click="isimg = false">取消</van-button>
    </div>
  </div>
</template>

<script>
// import {getInfo} from './user.js'
import { setInfo, setImg } from "./user.js";
import { mapState } from "vuex";
import moment from "moment";
import { VueCropper } from "vue-cropper";
export default {
  name: "bjzl",
  data() {
    return {
      isname: false,
      nametext: "",
      ispopup: false,
      isbirthday: false,
      isimg: false,
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),

      img: "", // 裁切图片
    };
  },
  components: {
    VueCropper,
  },
  methods: {
    nameClick() {
      this.isname = true;
      this.nametext = this.userInfo.name;
      this.$nextTick(() => {
        this.$refs.name.focus();
      });
    },
    xgname() {
      setInfo({ name: this.nametext.trim() }).then((res) => {
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          name: res.data.data.name,
        });
      });
      this.$toast.success("修改成功");
    },
    xbClick(gender) {
      setInfo({ gender }).then((res) => {
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          gender: res.data.data.gender ? 1 : 0,
        });
      });
      this.$toast.success("修改成功");
      this.ispopup = false;
    },
    srClick(value) {
      let birthday = moment(value).format("YYYY-MM-DD");
      setInfo({ birthday }).then((res) => {
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          birthday: res.data.data.birthday,
        });
      });
      this.$toast.success("修改成功");
      this.isbirthday = false;
    },
    xgimg(file) {
      this.img = file.content;
      this.isimg = true;
    },
    qdxgimg() {
      this.$refs.cropper.getCropBlob((data) => {
        let fd = new FormData();
        fd.append("photo", data);
        setImg(fd).then((res) => {
          this.$store.commit("setUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
        });
      });
      this.isimg = false;
    },
  },
  computed: {
    ...mapState(["userInfo"]),
  },
};
</script>

<style lang='less'>
.imgInfo {
  text-align: center;
  position: relative;
  .van-image {
    margin: 10px 0;
  }
  .van-uploader {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 120px;
    height: 120px;
    opacity: 0;
  }
}
.cropper-box {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  .van-button--primary {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .van-button--info {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>